package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.info.CardInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.EticketPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/12/15
 */

public class EticketModelImpl implements EticketModel, VolleyJsonObjectCallBack {
    EticketPresenterImpl eticketPresenter;

    @Override
    public void sendUrl(String url, EticketPresenterImpl eticketPresenter) {
        this.eticketPresenter = eticketPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    List<CardInfo> cardInfos = JsonUtil.getListFromString(response.getString("res"), CardInfo.class);
                    if (cardInfos.size() != 0) {
                        eticketPresenter.recCoupon(cardInfos.get(0));
                    }else {
                        eticketPresenter.recCoupon(null);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            eticketPresenter.sendError(error);
            }
        });
    }

    @Override
    public void doResUrl(String url, EticketPresenterImpl eticketPresenter) {
        this.eticketPresenter = eticketPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void getMsgUrl(String url, EticketPresenterImpl eticketPresenter) {

        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    RestaurantInfo restaurantInfo = JsonUtil.getObjectFromString(response.getString("res"), RestaurantInfo.class);
                    eticketPresenter.recMsg(restaurantInfo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            eticketPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            int res = response.getInt("res");
            eticketPresenter.recRes(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        eticketPresenter.sendError(error);
    }
}